package com.zt.controller;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ChannelContinuationTimeoutException;
import com.zt.pojo.JsonUtils;
import com.zt.pojo.Setmeal;
import com.zt.service.RecvService;
import com.zt.util.RedisClient;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RecvController {
    @Autowired
    private RecvService recvService;
    @Autowired
    private RedisClient redisClient;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue1"),
            exchange = @Exchange(name = "setmeal_exchange", type = ExchangeTypes.TOPIC),
            key = {"*.*"}
    )})
    public void add(String msg, Channel channel, Message message) throws IOException {
        Setmeal setmeal = JsonUtils.jsonToPojo(msg, Setmeal.class);
        recvService.add(setmeal);
        if (setmeal != null) {
            redisClient.set("setmeal_list" + setmeal.getId(), setmeal);
        }
        System.out.println(setmeal);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue2"),
            exchange = @Exchange(name = "setmeal_exchange2", type = ExchangeTypes.TOPIC),
            key = {"*.delete"}
    )})
    public void delete(Integer id, Channel channel, Message message) throws IOException {
        recvService.delete(id);
        redisClient.del("setmeal_list" + id);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println(id);
    }


    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue3"),
            exchange = @Exchange(name = "setmeal_exchange3", type = ExchangeTypes.TOPIC),
            key = {"a.update"}
    )})
    public void update(String setmeal, Channel channel, Message message) throws IOException {
        Setmeal setmeal1 = JsonUtils.jsonToPojo(setmeal, Setmeal.class);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        recvService.update(setmeal1);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue4"),
            exchange = @Exchange(name = "setmeal_exchange4", type = ExchangeTypes.TOPIC),
            key = {"a.get"}
    )})
    public void get(Integer id, Channel channel, Message message) throws IOException {
        Setmeal setmeal = recvService.get(id);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println(setmeal);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue5"),
            exchange = @Exchange(name = "setmeal_exchange5", type = ExchangeTypes.TOPIC),
            key = {"a.page"}
    )})
    public void page(Integer id, Channel channel, Message message) throws IOException {
        System.out.println(id);
        List<Setmeal> list=recvService.page();
        for (Setmeal setmeal : list) {
            System.out.println(setmeal);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue6"),
            exchange = @Exchange(name = "setmeal_exchange6", type = ExchangeTypes.TOPIC),
            key = {"a.like"}
    )})
    public void like(Integer id, Channel channel, Message message) throws IOException {
        List<Setmeal> list=recvService.like();
        for (Setmeal setmeal : list) {
            System.out.println(setmeal);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "setmeal_queue7"),
            exchange = @Exchange(name = "setmeal_exchange7", type = ExchangeTypes.TOPIC),
            key = {"a.between"}
    )})
    public void between(String map, Channel channel, Message message) throws IOException {
        Map map1 = JsonUtils.jsonToPojo(map, Map.class);
        String name= (String) map1.get("name");
        Integer min= (Integer) map1.get("min");
        Integer max= (Integer) map1.get("max");
        System.out.println(name+min+max);
        List<Setmeal> list=recvService.between(name,min,max);
        for (Setmeal setmeal : list) {
            System.out.println(setmeal);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
